package com.way.controller;

import com.way.domain.system.SysUser;
import com.way.dto.CommonResult;
import com.way.service.ISysUserService;
import com.way.util.MessageSourceService;
import com.way.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class SysUserController {

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private MessageSourceService messageSourceService;

    @GetMapping("/user/{loginName}")
    public SysUser findByUsername(@PathVariable("loginName") String loginName) {
        return sysUserService.getUser(loginName);
    }

    @PostMapping("/user")
    public CommonResult saveUser(@RequestBody SysUser user) {
        sysUserService.saveUser(user);
        return CommonResult.success();
    }

    @GetMapping("/userList")
    public CommonResult getUser() {
        PageUtil.startPage();
        List<SysUser> userList = sysUserService.getUserList();
        return CommonResult.success(PageUtil.pageResult(userList));
    }

    @GetMapping("/messages")
    public CommonResult messages() {
        String title = messageSourceService.getMessage("user.title");
        return CommonResult.success(title);
    }
}
